home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5233 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  124 lines

  1. Path: news.mindlink.net!uniserve!usenet
  2. From: nowher@anyplace.com (Chris)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help! I am baffled! (newbie) - problem.txt [1/1]
  5. Date: 3 Feb 1996 04:38:35 GMT
  6. Organization: UNIServe Online
  7. Distribution: world
  8. Message-ID: <4euosb$qnj@atlas.uniserve.com>
  9. NNTP-Posting-Host: van0300.tvs.net
  10. Mime-Version: 1.0
  11. Content-Type: multipart/mixed;
  12.      Boundary="*-*-*- Next Section -*-*-*"
  13. X-Newsreader: WinVN 0.99.2
  14.  
  15. --*-*-*- Next Section -*-*-*
  16.  
  17. I think I got a doozy here.
  18.  
  19. --*-*-*- Next Section -*-*-*
  20.  
  21.   I'll do my best to explain this problem.  The structure 
  22. below, Tpart, is declared in the global area of the program.
  23. The rest of the sample here is in a seperate function.
  24. Ok, in the first input block (PartNum), the input goes into 
  25. TempStr. Fine. With the section marked with ***** commented
  26. out, the output at the bottom is as expected. Whatever the 
  27. user types is output correctly. 
  28.  
  29.   The problem I have is when I de-comment the block marked by
  30. *****.  The output at the bottom is not correct. 
  31. TempPart.PartNum is somehow changed by the following input block.
  32. There are no explicit TempPart.PartNum= declarations, but the 
  33. output for PartNum is the same as Length. Except for this, my
  34. program so far works fine.
  35.  
  36. eg:  input by user:
  37.       PartNum:( PartA123   )
  38.        Length:( 123  )
  39.  
  40.    output by program:
  41. Part Num:123<-
  42. Length:123<-
  43.  
  44. when it *should* be:
  45. Part Num:PartA123<-
  46. Length:123    
  47.  
  48.   I am baffled by this. Please help.
  49.  
  50.  
  51.  
  52. Here are the relevant parts of my program:
  53.  
  54. typedef struct Tpart{
  55.          char *PartNum;           
  56.          unsigned int Length;     
  57.          unsigned int Width;      
  58.          char *Material;          
  59.          char Sugg;        
  60.                  Tpart *CrossRef1,*CrossRef2,*NextPart;
  61.            };      // sizeof=52 bytes
  62.  
  63. [snip]
  64.  
  65. Tpart *Head=PartHead;  // Local pointer to determine current position in list
  66. Tpart TempPart;        // Local var for input of field data
  67. char TempStr[20];
  68. char ch;
  69. int x=0;
  70.  
  71. // Init TempPart
  72. TempPart.PartNum = new char[15];
  73. TempPart.Length =0;
  74. TempPart.Width =0;
  75. TempPart.Material = new char[20];
  76. TempPart.Sugg =0;
  77.  
  78. [snip]
  79.  
  80. // take input for fields
  81. // ----input PartNum:
  82. gotoxy(21,5);   // position cursor at first field
  83. ch=' ';
  84. x=0;
  85. while (ch != '\r'){                  // by the way, this is my solution to the newbie
  86.   ch=getche();                       //  input problem of a few days ago. 
  87.   if (!iscntrl(ch) && wherex()<37) TempStr[x++]=ch;  
  88.     else switch(ch){
  89.       case '\r':TempStr[x]=NULL;break;
  90.       case '\b':x--;break;   
  91.       default:cout << '\b';beep();    // beep is a harmless function which does just that.
  92.       }
  93.   }
  94. TempPart.PartNum=TempStr;     
  95.  
  96. *****
  97. //-----input Length:
  98. gotoxy(21,6);   // position cursor at Length field
  99. ch=' ';
  100. x=0;
  101. while (ch != '\r'){
  102.   ch=getche();
  103.   if (isdigit(ch) && wherex()<27) TempStr[x++]=ch;
  104.     else switch(ch){
  105.       case '\r':TempStr[x]=NULL;break;
  106.       case '\b':x--;break;
  107.       case  '-':if (x==0)TempStr[x++]=ch;break;
  108.       default:cout << '\b';beep();
  109.       }
  110.   }
  111. TempPart.Length=atoi(TempStr);
  112.  
  113. *****
  114.  
  115. // the output here is for de-bugging purposes only.
  116. gotoxy(1,16);
  117. cout << "Part Num:" << TempPart.PartNum << "<-";
  118. cout << "\nLength:" << TempPart.Length << "<-";
  119. cout << "\nWidth:" << TempPart.Width << "<-";
  120. cout << "\nMaterial:" << TempPart.Material << "<-";
  121. cout << "\nOK? " << TempPart.Sugg << "<-";
  122. --*-*-*- Next Section -*-*-*--
  123.  
  124.